package com.wry.service.impl;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.wry.service.TemplateService;
import com.wry.vo.OrderSendBackB2bExportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <h1>  <h1>
 *
 * @author 魏如元
 * @since 2022/10/25
 */
@Service
@Slf4j
public class TemplateServiceImpl implements TemplateService {

    @Override
    public void templateImport(HttpServletResponse response) throws IOException {
        String fileName = "tableWrite" + System.currentTimeMillis() + ".xls";
        // 方法1 这里直接写多个table的案例了，如果只有一个 也可以直一行代码搞定，参照其他案
        // 这里 需要指定写用哪个class去写
        String path = this.getClass().getClassLoader().getResource("template/send_back_template.xls").getFile();

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.setContentType("application/x-download");
        ServletOutputStream out = response.getOutputStream();

        final ExcelWriter excelWriter = EasyExcelFactory.write(out).withTemplate(path).build();
        final WriteSheet writeSheet = EasyExcelFactory.writerSheet().build();

        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();

        excelWriter.fill(getOrder(), fillConfig, writeSheet);
        log.info("getOrderGoodsList() => {}", getOrderGoodsList());
        excelWriter.fill(getOrderGoodsList(), fillConfig, writeSheet);
        excelWriter.finish();
    }

    private OrderSendBackB2bExportVO getOrder() {
        OrderSendBackB2bExportVO o1 = new OrderSendBackB2bExportVO();
        o1.setRemark("导出哦");
        o1.setApplyPerson("大锤");
        o1.setApplyAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        o1.setSendBackNumber("TH202212036644");
        o1.setPredictPickUpAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return o1;
    }

    private List<OrderSendBackB2bExportVO.Goods> getOrderGoodsList() {
        List<OrderSendBackB2bExportVO.Goods> list = new ArrayList<>();
        OrderSendBackB2bExportVO.Goods goods1 = new OrderSendBackB2bExportVO.Goods();
        goods1.setIndex(1);
        goods1.setGoodsName("G1");
        goods1.setSendBackGoodsQuantity(10);
        OrderSendBackB2bExportVO.Goods goods2 = new OrderSendBackB2bExportVO.Goods();
        goods2.setIndex(2);
        goods2.setGoodsName("G2");
        goods2.setSendBackGoodsQuantity(11);
        OrderSendBackB2bExportVO.Goods goods3 = new OrderSendBackB2bExportVO.Goods();
        goods3.setIndex(3);
        goods3.setGoodsName("G3");
        goods3.setSendBackGoodsQuantity(1);
        list.add(goods1);
        list.add(goods2);
        list.add(goods3);
        return list;
    }
}
